package com.wwj.auction.login.controller;


import com.wwj.auction.common.result.R;
import com.wwj.auction.login.entity.OfferRecord;
import com.wwj.auction.login.entity.UserInfo;
import com.wwj.auction.login.entity.to.OfferRecordUserInfoTo;
import com.wwj.auction.login.entity.vo.OfferRecordDetailVo;
import com.wwj.auction.login.entity.vo.OfferRecordVo;
import com.wwj.auction.login.entity.vo.PageModel;
import com.wwj.auction.login.service.OfferRecordService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wwj
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/login/offerRecord")
@Api("出价记录")
public class OfferRecordController {

    @Autowired
    private OfferRecordService offerRecordService;

    /**
     * 保存买家的出价记录
     *
     * @param offerRecord
     * @return
     */
    @PostMapping("/offer")
    public R offer(@RequestBody OfferRecord offerRecord) {
        offerRecordService.saveRecord(offerRecord);
        return R.ok().message("出价成功");
    }

    /**
     * 分页查询出价记录
     *
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{userId}/{page}/{limit}")
    public R list(
            @PathVariable("userId") Integer userId,
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit) {
        PageModel<OfferRecordVo> pageModel = offerRecordService.listForRecord(userId, page, limit);
        return R.ok().data("pageModel", pageModel);
    }

    /**
     * 查询指定商品的详细出价情况
     *
     * @param userId
     * @param shopId
     * @return
     */
    @GetMapping("/list/by/{userId}/{shopId}")
    public R listById(
            @PathVariable("userId") Integer userId,
            @PathVariable("shopId") Integer shopId) {
        List<OfferRecordDetailVo> list = offerRecordService.listById(userId, shopId);
        return R.ok().data("list", list);
    }

    /**
     * 获取指定商品的最高出价者
     *
     * @param shopId
     * @return
     */
    @GetMapping("/first/{shopId}")
    public R first(@PathVariable("shopId") Integer shopId) {
        OfferRecordUserInfoTo userInfo = offerRecordService.first(shopId);
        return R.ok().data("userInfo", userInfo);
    }

    /**
     * 提供远程调用
     * 找到拍卖结束的商品最终成交的买家
     *
     * @param shopId
     * @return
     */
    @GetMapping("/find/buyer/{shopId}")
    public R findSeller(@PathVariable Integer shopId) {
        UserInfo userInfo = offerRecordService.findBuyer(shopId);
        return R.ok().data("userInfo", userInfo);
    }
}

